/**
 * 配置审核人员界面js
 */
 
 var applyTypeId;     //保存当前申请分类id 
 var examineLevelData;  //保存当前审核级别数据 
 
(function(){
	var col = [{field:'fcode',align:'center',title:'工号',width:60},
			   {field:'fname',align:'center',title:'姓名',width:60},
			   {field:'fksname',align:'center',title:'审批科室',width:60,
			   	formatter : function(value, rec) {
					return "<span title='"+value+"'>"+value+"</span>";
	        	}},
			   {field:'fgroupname',align:'center',title:'用户组',width:80}]
	
	//根据申请分类加载审核级别   
 	$('#examineLevel-data').datagrid({ 
 		toolbar:"#toolbar-examineLevel",
		fitColumns : true,
		border : false,
		striped : true,
		fit : true,
		singleSelect : true,
		onClickRow : loadExaminePersion,
		columns : [[{field:'orderV',align:'center',title:'流程顺序',width:60},
			   		{field:'fhj',align:'center',title:'环节',width:60},
			   		{field:'FNAME',align:'center',title:'审核流程名称',width:60}]]
 	});
 	
 	//根据审核级别加载该审核级别的人员
 	$('#examinePersion-data').datagrid({ 
 		toolbar:"#toolbar-examinePersion",
		fitColumns : true,
		border : false,
		striped : true,
		fit : true,
		singleSelect : true,
		onClickRow : loadPersionInfo,
		columns : [col]
 	});
 	
 	// 加载所有申请分类
	$("#apply-type").combobox({
		url : "systemMaintenance/loadApplyType",
		editable : false,
		valueField : "FID",
		textField : "FNAME",
		onLoadSuccess : function(data){
			if (data.length>0) {
				$("#apply-type").combobox('select',data[0].FID);   //默认勾选第一个申请分类
			}
		},
		onSelect : loadExamineLevel,  //根据选择的申请分类加载  审核界别
		onLoadError : errorPromt
	});
	
	//加载所有审批人员(从用户表中检索)
	$('#examine-Persion').combobox({
	    url:'systemMaintenance/loadUsers',
	    valueField:'FCODE',
	    textField:'FNAME',
	    width:140,
	    formatter : function(row) {
			var s = '<span style="font-weight:bold">' + row.FNAME + '</span><br/>' +
            '<span style="color:#888">' + row.FCODE + '</span>';
            return s;
		},
		onLoadError : errorPromt
	});
	
	// 科室下拉框
	$("#examine-Dept").combotree({
		url : "systemMaintenance/findWorkRooms",
		multiple:true,
		onLoadError : errorPromt
	})
})();

//根据申请分类id加载该分类下的审核级别
function loadExamineLevel(record){
	applyTypeId = record.FID; //将当前申请分类id保存至全局变量中
	
	$('#examineLevel-data').datagrid({
		url : "systemMaintenance/loadExamineLevel",
		queryParams : {fid : record.FID}
	});
}

//根据审核级别id加载对应的审核人员
function loadExaminePersion(index,row){
	examineLevelData = row;  //将当前的审核级别信息保存至全局变量中
	
	$('#examinePersion-data').datagrid({
		url : "systemMaintenance/loadExaminePersion",
		queryParams : {fid : row.FPX}  //因上下移动时 后台无法更新主键id，所以通过fpx与审核人员表进行关联
	});
	$('#level-name').textbox('setText',row.FNAME);
}

//新增审核级别 0 添加  1修改
function AddOrEditLevel(type){
	var examineName = $('#level-name').textbox('getText'); //获取审批级别名称
	
	if ("" == examineName) {
		$.messager.alert('提示','审批级别名称不能为空','info');
 		return;
	}
	var oldName="";  //修改前  记录之前的名称
	var urlStr = "";
	var datas;
	var name = "";
	//添加审批级别
	if (type == 0) {
		name = "添加";
		urlStr = "systemMaintenance/addExamineLevel";
		datas={examineName: examineName, applyTypeId: applyTypeId}
	}else{
		if (examineLevelData == null) {
			$.messager.alert('提示','请先选择要修改的审批级别','info');
 			return;
		}
		name = "修改";
		urlStr = "systemMaintenance/editExamineLevel";
		
		oldName = examineLevelData.FANME;
		datas={examineName: examineName, fid: examineLevelData.FID}
	}
	
	//判断是否存在相同的名称
	if (checkName(type, examineName, oldName)) {
		$.messager.alert('提示','该申请类型下存在相同审批名称','info');
 		return;
	}
	$.ajax({
		url : urlStr,
		dataType : 'json',
		type : 'post',
		data : datas,
		success : function(data){
			if (data) {
				$.messager.alert('提示',name+'成功','info');
				$('#level-name').textbox('setText','');
				$('#examineLevel-data').datagrid('reload');
			}else{
				$.messager.alert('提示',name+'失败','info');
			}
		},error : errorPromt
	})
}

//检测申请类型下是否存在相同的审批名称
function checkName(type,newName,oldName){
	var b = false; 
	
	$.ajax({
    	url : ctx +"/systemMaintenance/checkSpName",
    	async : false,
		dataType : 'json',
		type : 'post',
		data : {type: type, newName: newName, oldName: oldName},
		success : function(data){
		    //判断返回的数据是否存在
		    if (data.length>0) {
				b = true;
		    }else{
		    	b = false;
		    }
		},error : errorPromt
    })
    return b;
}

//审批级别上移和下移方法  0上移  1下移
function MoveLevel(type){
	var items = $('#examineLevel-data').datagrid('getSelected');
	if (items == null) {
		return;
	}
	//获取选择行的元素下标
	var index = $('#examineLevel-data').datagrid('getRowIndex',items);
	
	//获取所有行的长度
	var rows = $("#examineLevel-data").datagrid('getRows').length;
	//执行上移方法
	if (type == 0) {
		if (index != 0) {
			var toup = $('#examineLevel-data').datagrid('getData').rows[index];
            var todown = $('#examineLevel-data').datagrid('getData').rows[index - 1];
            
            //执行交换两者的fpx方法
            ExchangeFpx(toup.FID, toup.FPX, todown.FID, todown.FPX);
            
            var toupName = toup.FNAME;
            var toupFpx = toup.FPX;
            //交换两者的名称
            $('#examineLevel-data').datagrid('getData').rows[index].FNAME = todown.FNAME;
            $('#examineLevel-data').datagrid('getData').rows[index - 1].FNAME = toupName;
            
            //交换两者的排序号
            $('#examineLevel-data').datagrid('getData').rows[index].FPX = todown.FPX;
            $('#examineLevel-data').datagrid('getData').rows[index - 1].FPX = toupFpx;
            
            //刷新行，并且选中刚操作的行
            $('#examineLevel-data').datagrid('refreshRow', index);
            $('#examineLevel-data').datagrid('refreshRow', index - 1);
            
            $('#examineLevel-data').datagrid('selectRow', index - 1);
            //自动执行点击方法
            loadExaminePersion(index-1, $('#examineLevel-data').datagrid('getData').rows[index - 1]);
		}
	}else{
		if (index != rows - 1) {
			var todown = $('#examineLevel-data').datagrid('getData').rows[index];
            var toup = $('#examineLevel-data').datagrid('getData').rows[index + 1];
            
            //执行交换两者的fpx方法
            ExchangeFpx(todown.FID, todown.FPX, toup.FID, toup.FPX);
            
            var toupName = toup.FNAME;
            var toupFpx = toup.FPX;
            //交换两者的名称
            $('#examineLevel-data').datagrid('getData').rows[index + 1].FNAME = todown.FNAME;
            $('#examineLevel-data').datagrid('getData').rows[index].FNAME = toupName;
            
            //交换两者排序号
            $('#examineLevel-data').datagrid('getData').rows[index + 1].FPX = todown.FPX;
            $('#examineLevel-data').datagrid('getData').rows[index].FPX = toupFpx;
            
            //刷新行，并且选中刚操作的行
            $('#examineLevel-data').datagrid('refreshRow', index);
            $('#examineLevel-data').datagrid('refreshRow', index + 1);
            $('#examineLevel-data').datagrid('selectRow', index + 1);
            
            //自动执行点击方法
			loadExaminePersion(index+1, $('#examineLevel-data').datagrid('getData').rows[index + 1]);
		}
	}
}

//上移和下移时交换两者的fpx号 (当前行id，当前行fpx号，交换行id，交换行fpx号)
function ExchangeFpx(currentId, currentFpx, nextId, nextFpx){
	$.ajax({
		url : ctx +"/systemMaintenance/ExchangeFpx",
    	async : false,
		dataType : 'json',
		type : 'post',
		data : {currentId: currentId, currentFpx: currentFpx, nextId: nextId, nextFpx: nextFpx},
		error : errorPromt
	})
}

//根据审批级别id删除
function DeleteLevel(){
	var items = $('#examineLevel-data').datagrid('getSelected');
	if (items == null) {
		$.messager.alert('提示','请选择要删除的','info');
		return;
	}
	$.messager.confirm('信息提示', '确定要删除'+items.FNAME+'该记录？', function (result) {
		if (result) {
			$.ajax({
		    	url : ctx +"/systemMaintenance/deleteExamineLevel",
				dataType : 'json',
				type : 'post',
				data : {fid: items.FPX},
				success : function(data){
				    if (data) {
						$.messager.alert('提示','删除成功','info');
						$('#examineLevel-data').datagrid('reload');
				    }else{
				    	$.messager.alert('提示','删除失败','info');
				    }
				},error : errorPromt
		    })
		}
	});
}

//添加或修改审核级别下人员 0添加  1修改
function AddOrEditPersion(name){
	//获取用户的工号
	var persionCode = $('#examine-Persion').combobox('getValue');
	
	//获取审批科室科号
	var examineKhs = $("#examine-Dept").combotree('getValues');
	var examineKsName = $("#examine-Dept").combotree('getText');
	
	var examineKh = "";
	for (var i = 0; i < examineKhs.length; i++) {
		if (i == examineKhs.length-1) {
			examineKh += "''"+examineKhs[i]+"''";
		}else{
			examineKh += "''"+examineKhs[i]+"'',";
		}
		
	}
	var count = 0;  //记录在列表中出现相同fcode次数
	var rows = $('#examinePersion-data').datagrid('getRows');//获取所有选择的行
	
	var urlStr = "";
	var datas;
	if (name == '添加') {
		if (examineLevelData == null) {
			$.messager.alert('提示','请先选择相应的审批级别','info');
			return;
		}
		
		for (var i = 0; i < rows.length; i++) {
			if (persionCode == rows[i].fcode) {  //排除修改前的用户fcode
				count ++;
				break;
			}
		}
		
		//判断如果count不等0  既存在相同的用户fcode时
		if (count != 0) {
			$.messager.alert('提示','列表中已经存在相同的审批人员','info');
			return;
		}
		
		urlStr = ctx +"/systemMaintenance/addExaminePersion";
		datas = {persionCode: persionCode, fspks: examineKh, fspjbid: examineLevelData.FPX, examineKsName: examineKsName};
		
	}else{
		var items = $('#examinePersion-data').datagrid('getSelected');
		if (items == null) {
			$.messager.alert('提示','请选择要修改的数据','info');
			return;
		}
		
		for (var i = 0; i < rows.length; i++) {
			if (persionCode == rows[i].fcode && persionCode != items.fcode) {  //排除修改前的用户fcode
				count ++;
				break;
			}
		}
		
		//判断如果count不等0  既存在相同的用户fcode时
		if (count != 0) {
			$.messager.alert('提示','列表中已经存在相同的审批人员','info');
			return;
		}
		
		urlStr = ctx +"/systemMaintenance/editExaminePersion";
		datas = {persionCode: persionCode, fspks: examineKh, fid: items.FID, oFcoe: items.fcode, examineKsName: examineKsName};
	}
	
	if (examineKh.length == 0) {
		$.messager.alert('提示','请选择相应的审批科室','info');
		return;
	}
	if ("" == persionCode) {
		$.messager.alert('提示','请选择审批人员','info');
		return;
	}
	
	$.ajax({
    	url : urlStr,
		dataType : 'json',
		type : 'post',
		data : datas,
		success : function(data){
		    if (data) {
				$.messager.alert('提示',name+'成功','info');
				$('#examinePersion-data').datagrid('reload');
		    }else{
		    	$.messager.alert('提示',name+'失败','info');
		    }
		},error : errorPromt
    })
	
}

//根据审核级别下人员id删除该数据
function DeletePersion(){
	var items = $('#examinePersion-data').datagrid('getSelected');
	if (items == null) {
		$.messager.alert('提示','请选择要修改的数据','info');
		return;
	}
	
	$.ajax({
    	url : ctx +"/systemMaintenance/deleteExaminePersion",
		dataType : 'json',
		type : 'post',
		data : {fid: items.FID},
		success : function(data){
		    if (data) {
				$.messager.alert('提示','删除成功','info');
				$('#examinePersion-data').datagrid('reload');
				
				$('#examine-Dept').combotree('clear');    //清除combotree的值
				$('#examine-Persion').combobox('setValue',''); 
				
		    }else{
		    	$.messager.alert('提示','删除失败','info');
		    }
		},error : errorPromt
    })
}

function loadPersionInfo(index, row){
	
	$('#examine-Persion').combobox('setValue',row.fcode); //勾选当前用户信息框
	$('#examine-Dept').combotree('clear');    //清除combotree的值
	
	var khArray = row.fspks.replace(new RegExp(/(')/g),'').split(',');  //分割科号
	
	var t = $('#examine-Dept').combotree('tree');	// 获取树对象
	
	for (var i = 0; i < khArray.length; i++) {   //遍历科号数组  勾选对应的框
		var node = t.tree('find', khArray[i]);
		t.tree('check', node.target);
	}
}